<template>
  <div class="contain">
    <!-- 图片上传组件辅助-->
    <el-upload class="uploadName" :action="serverUrl" name="imgFile" :show-file-list="false" :on-success="uploadSuccess" :on-error="uploadError" :before-upload="beforeUpload" :limit="1"></el-upload>
    <!--富文本编辑器组件-->
    <el-row v-loading="quillUpdateImg">
      <quill-editor v-model="editform.content" ref="myQuillEditor" :options="editorOption" @change="onEditorChange($event)"></quill-editor>
    </el-row>
  </div>
</template>

<script>
// 工具栏配置
const toolbarOptions = [
  ["bold", "italic", "underline", "strike"], // toggled buttons
  ["blockquote", "code-block"],

  [{ header: 1 }, { header: 2 }], // custom button values
  [{ list: "ordered" }, { list: "bullet" }],
  [{ script: "sub" }, { script: "super" }], // superscript/subscript
  [{ indent: "-1" }, { indent: "+1" }], // outdent/indent
  [{ direction: "rtl" }], // text direction

  [{ size: ["small", false, "large", "huge"] }], // custom dropdown
  [{ header: [1, 2, 3, 4, 5, 6, false] }],

  [{ color: [] }, { background: [] }], // dropdown with defaults from theme
  [{ font: [] }],
  [{ align: [] }],
  ["link", "image", "video"],
  ["clean"] // remove formatting button
];
import global from "@/components/global";
export default {
  props: {
    type: Object,
    editform: {
      default: () => {
        return {
          content: "",
          imgUrl: ""
        };
      }
    }
    //     uploadName: {
    //       type: String,
    //       default: "uploader"
    //     }
  },
  data() {
    return {
      serverUrl: `${global.serverSrc}/upload`,
      quillUpdateImg: false,
      editorOption: {
        placeholder: "请输入文章内容",
        theme: "snow", // or 'bubble'
        modules: {
          toolbar: {
            container: toolbarOptions, // 工具栏
            handlers: {
              image: value => {
                if (value) {
                  console.log(11);
                  document
                    .querySelector(".uploadName  input")
                    .click();
                } else {
                  this.quill.format("image", false);
                }
              }
            }
          }
        }
      }
    };
  },
  methods: {
    beforeUpload() {
      // 显示loading动画
      this.quillUpdateImg = true;
    },

    uploadSuccess(res, file) {
      // res为图片服务器返回的数据
      // 获取富文本组件实例
      let quill = this.$refs['myQuillEditor'].quill;
      console.log(quill);
      // 如果上传成功
      console.log(res)
      if (res && res.code == "00") {
        // 获取光标所在位置

        let length = quill.getSelection().index;

        // 插入图片  res.info为服务器返回的图片地址
        quill.insertEmbed(length, "image", global.serverSrc + res.msg);
        // 调整光标到最后
        quill.setSelection(length + 1);
        this.editform.imgUrl = res.msg;
      } else {
        this.$message.error("图片插入失败");
      }
      // loading动画消失
      this.quillUpdateImg = false;
    },

    // 富文本图片上传失败
    uploadError() {
      // loading动画消失
      this.quillUpdateImg = false;
      this.$message.error("图片插入失败");
    },

    //编辑器内容发生改变时触发
    onEditorChange($event) {
      this.editform.content = $event.html;
      this.$emit("quillEdit", this.editform.content);
    }
  }
};
</script>

<style scoped >
.uploadintro {
  height: 0 !important;
}
</style>